/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ld.security.auth.shiro.login.jwt;

import com.ld.security.auth.common.sys.entity.SysPerEntity;
import com.ld.security.auth.common.sys.entity.SysRoleEntity;
import com.ld.security.auth.common.sys.login.UserService;
import com.ld.security.auth.common.sys.vo.UserDetailsVo;
import com.ld.security.auth.shiro.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Shiro 授权认证
 *
 * @author geekidea
 * @date 2019-09-27
 * @since 1.3.0.RELEASE
 **/
@Slf4j
public class JwtRealm extends AuthorizingRealm {


    @Autowired
    private JwtProperties jwtProperties;

    private UserService userAuthService;

    public UserService getUserAuthService() {
        return userAuthService;
    }

    public void setUserAuthService(UserService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token != null && token instanceof JwtToken;
    }

    /**
     * 授权认证,设置角色/权限信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();

        log.debug("doGetAuthorizationInfo principalCollection...");

        // 设置角色/权限信息
        String token = principalCollection.toString();
        // 获取username
        String username = JwtUtil.getUsername(token);

        UserDetailsVo detailsVo =  userAuthService.getUserDetails(username);

        for (SysRoleEntity sysRoleEntity : detailsVo.getRoles()) {
            info.addRole(sysRoleEntity.getCode());
        }
        for (SysPerEntity sysPerEntity : detailsVo.getPers()) {
            info.addStringPermission(sysPerEntity.getName());
        }
        return  info;
    }

    /**
     * 登陆认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.debug("doGetAuthenticationInfo authenticationToken...");
        // 校验token
        JwtToken jwtToken = (JwtToken) authenticationToken;
        if (jwtToken == null) {
            throw new AuthenticationException("jwtToken不能为空");
        }

        return new SimpleAuthenticationInfo(
                jwtToken,
                jwtToken,
                getName()
        );

    }

}
